<?php

// +----------------------------------------------------------------------
// | Easyadmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\statistics;


//公共

use app\admin\model\website\Rooms;
use app\admin\model\website\Users as UsersModel;
use app\common\controller\AdminController;
use broadcast\redis\setRedis;
use think\App;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="站点管理")
 */
class Area extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id' => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\OnlineRecord();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();

            if (!isset($params['start_time']) or empty($params['start_time'])) {
                $params['start_time'] = date('Y-m-d 00:00:00');
            }

            if (!isset($params['end_time']) or empty($params['end_time'])) {
                $params['end_time'] = date('Y-m-d 23:59:59');
            }

            $chars_json = file_get_contents("chars.json");
            $chars = json_decode($chars_json, true);

            $area_json = file_get_contents("area.json");
            $area = json_decode($area_json, true);
            $data = [];
            $total_nums = 0;
            $max_num = 0;
            foreach ($area as $item) {

                $nums = $this->model::where('start_time', '>=', strtotime($params['start_time']))->where('end_time', '<=', strtotime($params['end_time']))->whereLike('home', "%$item%")->count();
                if($nums > $max_num){
                    $max_num = $nums;
                }
                $data[] = ['name' => $item, 'value' => $nums];
                $total_nums += $nums;
            }
            $data = compact('chars', 'params', 'data', 'total_nums','max_num');
            return json($data);
        }
        return $this->fetch();
    }
}
